package com.example.dql.diary.Suspending;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dql.diary.Model.SusContentDTO;
import com.example.dql.diary.Model.SuspendingDTO;
import com.example.dql.diary.R;
import com.example.dql.diary.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import qdx.indexbarlayout.IndexBar;
import qdx.indexbarlayout.IndexLayout;

public class TelePhoenActivity extends BaseActivity {

	@BindView(R.id.index_layout)
	IndexLayout indexLayout;
	@BindView(R.id.recyclerView)
	RecyclerView recyclerView;
	private List<SuspendingDTO> textList = new ArrayList<>();
	private List<SusContentDTO> tv = new ArrayList<>();
	private List<String> tList = new ArrayList<>();
	private SuspendingAdapter adapter;

	@Override
	public int getLayoutId() {
		return R.layout.activity_tele_phoen;
	}

	@Override
	public Activity bindActivity() {
		return this;
	}

	@Override
	public void init() {
		setRecycleData();
		final LinearLayoutManager manager = new LinearLayoutManager(this);
		recyclerView.setLayoutManager(manager);
		adapter = new SuspendingAdapter(null);
		NormalDecoration decoration = new NormalDecoration() {
			@Override
			public String getHeaderName(int position) {
				return textList.get(position).getText();
			}
		};
		//自定义布局
		decoration.setOnDecorationHeadDraw(new NormalDecoration.OnDecorationHeadDraw() {
			@Override
			public View getHeaderView(final int position) {
				final View headView = LayoutInflater.from(TelePhoenActivity.this).inflate(R.layout.item_head, null);
				final TextView tvAvatar = (TextView) headView.findViewById(R.id.tv_sus_head);
				tvAvatar.setText(textList.get(position).getText());
//                decoration.loadImage(carList.get(pos).getLetter(), pos, ivAvatar);
				return headView;
			}
		});
		decoration.setOnHeaderClickListener(new NormalDecoration.OnHeaderClickListener() {
			@Override
			public void headerClick(int position) {
				Toast.makeText(TelePhoenActivity.this, "点击到头部" + textList.get(position).getText(), Toast.LENGTH_SHORT).show();
			}
		});
		adapter.addData(tv);
		recyclerView.addItemDecoration(decoration);
		recyclerView.setAdapter(adapter);



		indexLayout.setIndexBarHeightRatio(0.9f);
		indexLayout.setCircleDuration(200);//触摸显示的字母圆显示时间
		indexLayout.getIndexBar().setSelTextColor(0xFF000000);
		indexLayout.getIndexBar().setIndexsList(tList);//设置首字母信息
		indexLayout.getIndexBar().setIndexChangeListener(new IndexBar.IndexChangeListener() {
			@Override
			public void indexChanged(String indexName) {
				for (int i = 0; i < textList.size(); i++) {
					if (indexName.equals(textList.get(i).getText())) {
						manager.scrollToPositionWithOffset(i, 0);
						return;
					}
				}
			}
		});

	}

	private void setRecycleData() {
		SuspendingDTO a = new SuspendingDTO("A");
		textList.add(a);
		SuspendingDTO b = new SuspendingDTO("B");
		textList.add(b);
		SuspendingDTO c = new SuspendingDTO("C");
		textList.add(c);
		SuspendingDTO d = new SuspendingDTO("D");
		textList.add(d);
		SuspendingDTO e = new SuspendingDTO("E");
		textList.add(e);
		SuspendingDTO f = new SuspendingDTO("F");
		textList.add(f);
		SuspendingDTO g = new SuspendingDTO("G");
		textList.add(g);
		SuspendingDTO h = new SuspendingDTO("H");
		textList.add(h);
		SuspendingDTO i = new SuspendingDTO("I");
		textList.add(i);
		SuspendingDTO j = new SuspendingDTO("J");
		textList.add(j);
		SuspendingDTO k = new SuspendingDTO("K");
		textList.add(k);
		SuspendingDTO l = new SuspendingDTO("L");
		textList.add(l);
		tList.add("A");
		tList.add("B");
		tList.add("C");
		tList.add("D");
		tList.add("E");
		tList.add("F");
		tList.add("G");
		tList.add("H");
		tList.add("I");
		tList.add("J");
		tList.add("K");
		tList.add("L");
		for (int q = 0 ;q < 12 ; q++){
			SusContentDTO t = new SusContentDTO("大哥大嫂过年好，你是我的爷，我是你的儿");
			tv.add(t);
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO: add setContentView(...) invocation
		ButterKnife.bind(this);
	}
}
